package com.hngates.project.service.impl;

import java.util.List;
import java.util.Objects;

import com.hngates.common.utils.DateUtils;
import com.hngates.project.domain.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hngates.project.mapper.SubcontractorSpecificMapper;
import com.hngates.project.domain.SubcontractorSpecific;
import com.hngates.project.service.ISubcontractorSpecificService;
import com.hngates.common.core.text.Convert;

/**
 * 分包单位具体信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-24
 */
@Service
public class SubcontractorSpecificServiceImpl implements ISubcontractorSpecificService {
    @Autowired
    private SubcontractorSpecificMapper subcontractorSpecificMapper;

    /**
     * 查询分包单位具体信息
     *
     * @param id 分包单位具体信息主键
     * @return 分包单位具体信息
     */
    @Override
    public SubcontractorSpecific selectSubcontractorSpecificById(Long id) {
        return subcontractorSpecificMapper.selectSubcontractorSpecificById(id);
    }

    /**
     * 查询分包单位具体信息列表
     *
     * @param subcontractorSpecific 分包单位具体信息
     * @return 分包单位具体信息
     */
    @Override
    public List<SubcontractorSpecific> selectSubcontractorSpecificList(SubcontractorSpecific subcontractorSpecific) {
        return subcontractorSpecificMapper.selectSubcontractorSpecificList(subcontractorSpecific);
    }

    /**
     * 新增分包单位具体信息
     *
     * @param subcontractorSpecific 分包单位具体信息
     * @return 结果
     */
    @Override
    public int insertSubcontractorSpecific(SubcontractorSpecific subcontractorSpecific) {
        subcontractorSpecific.setCreateTime(DateUtils.getNowDate());
        return subcontractorSpecificMapper.insertSubcontractorSpecific(subcontractorSpecific);
    }

    /**
     * 修改分包单位具体信息
     *
     * @param subcontractorSpecific 分包单位具体信息
     * @return 结果
     */
    @Override
    public int updateSubcontractorSpecific(SubcontractorSpecific subcontractorSpecific) {
        subcontractorSpecific.setUpdateTime(DateUtils.getNowDate());
        return subcontractorSpecificMapper.updateSubcontractorSpecific(subcontractorSpecific);
    }

    /**
     * 批量删除分包单位具体信息
     *
     * @param ids 需要删除的分包单位具体信息主键
     * @return 结果
     */
    @Override
    public int deleteSubcontractorSpecificByIds(String ids) {
        return subcontractorSpecificMapper.deleteSubcontractorSpecificByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除分包单位具体信息信息
     *
     * @param id 分包单位具体信息主键
     * @return 结果
     */
    @Override
    public int deleteSubcontractorSpecificById(Long id) {
        return subcontractorSpecificMapper.deleteSubcontractorSpecificById(id);
    }

    /**
     * @param subcontractorSpecificList 分包单位具体信息
     * @param updateSupport             是否支持更新 如果已经存在 则进行更新数据
     * @param operName                  操作用户
     * @return
     */
    @Override
    public String importSubcontractorSpecific(List<SubcontractorSpecific> subcontractorSpecificList, Boolean updateSupport, String operName) {
        if (Objects.isNull(subcontractorSpecificList) || subcontractorSpecificList.size() == 0) {
            throw new BusinessException("分包单位具体信息不能为空");
        }
        StringBuilder importMsg = new StringBuilder();
        //循环Excel中的记录
        for (SubcontractorSpecific subcontractorSpecific : subcontractorSpecificList) {
            try {
                //constructionUnit是循环中 当前的记录对象
                //根据当前的Excel中的对象去数据库中查 查询时 以Excel中名称作为参数去查 返回值是 与项目同名的记录转为数据库对象
                SubcontractorSpecific subcontractorSpecific1 = subcontractorSpecificMapper.selectSubcontractorSpecificByBalanceDue(subcontractorSpecific.getBalanceDue());
                //判断数据库对象是否存在
                if (subcontractorSpecific1 == null) {
                    //如果不存在就创建
                    subcontractorSpecific1 = new SubcontractorSpecific();
                    //给对象的字段名赋值
                    subcontractorSpecific1.setBalanceDue(subcontractorSpecific.getBalanceDue());
                    subcontractorSpecific1.setDefaultNumber(subcontractorSpecific.getDefaultNumber());
                    subcontractorSpecific1.setWorkerSource(subcontractorSpecific.getWorkerSource());
                    subcontractorSpecific1.setWorkerRepresentative(subcontractorSpecific.getWorkerRepresentative());
                    subcontractorSpecific1.setPhone(subcontractorSpecific.getPhone());

                    //赋值完成后 将这个对象插入到数据库中
                    subcontractorSpecificMapper.insertSubcontractorSpecific(subcontractorSpecific1);
                    importMsg.append("<br/>" + "分包单位具体信息" + subcontractorSpecific1.getBalanceDue() + "导入成功");

                } else if (updateSupport) {

                    subcontractorSpecific1.setUpdateBy(operName);
                    subcontractorSpecific1.setDefaultNumber(subcontractorSpecific.getDefaultNumber());
                    subcontractorSpecific1.setWorkerSource(subcontractorSpecific.getWorkerSource());
                    subcontractorSpecific1.setWorkerRepresentative(subcontractorSpecific.getWorkerRepresentative());
                    subcontractorSpecific1.setPhone(subcontractorSpecific.getPhone());
                    subcontractorSpecificMapper.updateSubcontractorSpecific(subcontractorSpecific1);

                    importMsg.append("<br/>" + "分包单位具体信息" + subcontractorSpecific1.getBalanceDue() + "更新成功");
                } else {

                    importMsg.append("<br/>" + "分包单位具体信息" + subcontractorSpecific1.getBalanceDue() + "已存在");
                }

            } catch (Exception e) {
                importMsg.append("<br/>" + "分包单位具体信息" + subcontractorSpecific.getBalanceDue() + "导入时异常");

            }
        }
        return importMsg.toString();
    }
}
